#include<iostream>

using namespace std;

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long long l,r;
		cin>>l>>r;
		long long s=r/2+1;
		if(l>s) printf("%lld\n",r%l);
		else printf("%lld\n",r%s);
	}
	return 0;
}
